import 'dart:io';

import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mahjong/entity/Restful.dart';
import 'package:mahjong/entity/UserEntityInfo.dart';
import 'package:mahjong/util/DictionaryConst.dart';
import 'package:mahjong/util/EncryptHelper.dart';
import 'package:mahjong/util/Http.dart';
import 'package:mahjong/util/RegExpConstant.dart';
import 'package:mahjong/util/CustomerDialog.dart';
import 'package:mahjong/widget/CommonAlterDialog.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mahjong/widget/ITextFiled.dart';

class RegisterAccount extends StatefulWidget {
  @override
  _RegisterAccountState createState() => _RegisterAccountState();
}

class _RegisterAccountState extends State<RegisterAccount> {
  TextFiledValidata _userNameVlidata;
  TextFiledValidata _pwdVlidata;
  bool readAgreement = false;
  @override
  void initState() {
    super.initState();
    _userNameVlidata = TextFiledValidata.init(
        show: false, tips: "4-16位字符", paretten: RegExpConstant.userName);
    _pwdVlidata = TextFiledValidata.init(
        show: false, tips: "6-20位至少带有数字和字母", paretten: RegExpConstant.password);
  }

  @override
  Widget build(BuildContext context) {
    return CommonAlterDialog(
      backgroundColor: Colors.white,
      child: Scrollbar(
        child: SingleChildScrollView(
          physics: NeverScrollableScrollPhysics(),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              SizedBox(
                height: 20.h,
              ),
              ITextFiled(
                400.w,
                60.h,
                hintText: "请输入用户名",
                obscureText: false,
                preIcon: Icons.account_box,
                textFiledValidata: _userNameVlidata,
              ),
              SizedBox(
                height: 10.h,
              ),
              ITextFiled(
                400.w,
                60.h,
                hintText: "请输入密码",
                obscureText: true,
                preIcon: Icons.fingerprint,
                textFiledValidata: _pwdVlidata,
              ),
              SizedBox(
                height: 10.h,
              ),
              ITextFiled(
                400.w,
                60.h,
                hintText: "请输入邮箱",
                obscureText: false,
                preIcon: Icons.email,
              ),
              SizedBox(
                height: 10.h,
              ),
              Container(
                  color: readAgreement
                      ? Colors.blue.withOpacity(0.8)
                      : Colors.grey,
                  width: 400.w,
                  height: 40.h,
                  child: OutlinedButton(
                    child: Text(
                      "注册",
                      style: TextStyle(color: Colors.white, fontSize: 20.sp),
                    ),
                    onPressed: () {
                      if (!readAgreement) {
                        return;
                      }
                      if (_userNameVlidata.check() && _pwdVlidata.check()) {
                        CustomerDialog.showVerification(context, (data) async {
                          String decodePwd =
                              await EncryptHelper.encode(_pwdVlidata.value);
                          UserEntityInfo userInfo = UserEntityInfo(
                              verificationMode: data,
                              sysUser: SysUser.init(
                                  _userNameVlidata.value, decodePwd));
                          Http.getInstance().request<Restful>(
                              DictionaryConst.userCreate,
                              type: RequestEnum.POST,
                              contentType: ContentType.json,
                              showLoading: true,
                              loadingMsg: "注册中...",
                              data: userInfo.toString(), success: (data) {
                            BotToast.showText(text: data.body);
                          }, error: (data) {
                            BotToast.showText(text: data.msg);
                            setState(() {});
                          });
                        });
                      } else {
                        setState(() {});
                      }
                    },
                  )),
              SizedBox(
                height: 10.h,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Checkbox(
                      value: readAgreement,
                      onChanged: (value) {
                        print(readAgreement);
                        readAgreement = !readAgreement;
                        setState(() {});
                      }),
                  Text("已阅读并同意"),
                  Text("《服务协议》",
                      style: TextStyle(color: Colors.blue, fontSize: 20.sp))
                ],
              )
            ],
          ),
        ),
      ),
    );
  }
}
